package test.rocketmq;

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.threads.JMeterContext;
import org.apache.jmeter.threads.JMeterContextService;

import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.client.producer.SendStatus;
import com.alibaba.rocketmq.common.message.Message;

/**
 * 用于Rocketmq生产者测试 ，三个topic
 * @author yylc-Y02673
 *需要传送4个值   
 * 1.server:port
 * 2.message1 message2 message3
 */
public class RocketMQProducer extends AbstractJavaSamplerClient{
	private String nameServer ;
	private String message1;
	private String message2;
	private String message3;
	
	
	
	
	@Override
	public Arguments getDefaultParameters() {
		Arguments params = new Arguments();
		params.addArgument("server:port","");
		params.addArgument("message1","");
		params.addArgument("message2","");
		params.addArgument("message3","");
		return params;
	}
	
	@Override
	public void setupTest(JavaSamplerContext context) {
		if (context.containsParameter("server:port")) {
			this.nameServer = context.getParameter("server:port");
		}
		if (context.containsParameter("message1")) {
			this.message1 = context.getParameter("message1");
		}
		
		if (context.containsParameter("message2")) {
			this.message2 = context.getParameter("message2");
		}
		
		if (context.containsParameter("message3")) {
			this.message3 = context.getParameter("message3");
		}
		
	}
	
	
	
	
	

	@Override
	public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
		
		SampleResult sr = new SampleResult();
		sr.setSampleLabel("RocketMQ测试");
		try {
			sr.sampleStart();
			DefaultMQProducer producer = new DefaultMQProducer(MixUtil.createGroupName("ProducerGroupName_"));
			
			producer.setNamesrvAddr(nameServer);//获取服务器地址
			producer.setInstanceName("Producer");//实例名
			// producer.
			//producer.setVipChannelEnabled(false);
			
			producer.start();// once  
			int times = 1000;
			for(int i=0;i<times;++i){
				try {
					{
						Message msg = new Message("Message1Topic", "TagA", message1.getBytes());
						SendResult sendResult = producer.send(msg);
						if(sendResult ==null || sendResult.getSendStatus() != SendStatus.SEND_OK){
							System.err.println(sendResult);
						}
					}
					//Message message = new Message(topic, tags, keys, body)
					//Message message = new Message(topic, body)
					
					{
						Message msg = new Message("Message2Topic", "TagB",  message2.getBytes());
						SendResult sendResult = producer.send(msg);
						if(sendResult ==null || sendResult.getSendStatus() != SendStatus.SEND_OK){
							System.err.println(sendResult);
						}
					}
					
					{
						Message msg = new Message("Message3Topic", "TagC",  message3.getBytes());
						SendResult sendResult = producer.send(msg);
						if(sendResult ==null || sendResult.getSendStatus() != SendStatus.SEND_OK){
							System.err.println(sendResult);
						}
					}
					
				}catch (Exception e){
					e.printStackTrace();;
				}
			}
			
			producer.shutdown();
			
			sr.setResponseData("success","utf-8");
			sr.setDataType(SampleResult.TEXT);
			sr.setSuccessful(true);
		}catch(Exception e){
			sr.setSuccessful(false);
			sr.setResponseData(MixUtil.getStackTrace(e),"utf-8");
			e.printStackTrace();
		}
		finally {
			
			sr.sampleEnd();
		}
		
		
		return sr;
	}
	
	/**
	 * 因为未知配置原因，只能手工创建topic  https://blog.csdn.net/chenaima1314/article/details/79403113
	 * 具体问题待排查
	 */
/*
	@Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
		 
        SampleResult sr = new SampleResult();
        sr.setSampleLabel("RocketMQ测试");
        try {
            sr.sampleStart();
            DefaultMQProducer producer = new DefaultMQProducer(MixUtil.createGroupName("ProducerGroupName_"));
            producer.setNamesrvAddr(MixUtil.NAME_SRV_ADDR);//获取服务器地址
            producer.setInstanceName("Producer");//实例名
           // producer.
           //producer.setVipChannelEnabled(false);
 
            producer.start();// once  
            int times = 1000;
            for(int i=0;i<times;++i){
                try {
                    {
                        Message msg = new Message("Message1Topic", "TagA", message1.getBytes());
                        SendResult sendResult = producer.send(msg);
                        if(sendResult ==null || sendResult.getSendStatus() != SendStatus.SEND_OK){
                            System.err.println(sendResult);
                        }
                    }
                    	//Message message = new Message(topic, tags, keys, body)
                    	//Message message = new Message(topic, body)
                    	
                    {
                        Message msg = new Message("Message2Topic", "TagB",  message2.getBytes());
                        SendResult sendResult = producer.send(msg);
                        if(sendResult ==null || sendResult.getSendStatus() != SendStatus.SEND_OK){
                            System.err.println(sendResult);
                        }
                    }
 
                    {
                        Message msg = new Message("Message3Topic", "TagC",  message3.getBytes());
                        SendResult sendResult = producer.send(msg);
                        if(sendResult ==null || sendResult.getSendStatus() != SendStatus.SEND_OK){
                            System.err.println(sendResult);
                        }
                    }
 
                }catch (Exception e){
                    e.printStackTrace();;
                }
            }
 
            producer.shutdown();
 
            sr.setResponseData("success","utf-8");
            sr.setDataType(SampleResult.TEXT);
            sr.setSuccessful(true);
        }catch(Exception e){
            sr.setSuccessful(false);
            sr.setResponseData(MixUtil.getStackTrace(e),"utf-8");
            e.printStackTrace();
        }
        finally {
 
            sr.sampleEnd();
        }
 
 
        return sr;
    }
    */
	
	
	

}
